﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	public partial class FormObtainLocation : System.Windows.Forms.Form
	{
		public FormObtainLocation(string locationName)
		{
			this.InitializeComponent();
			base.Icon = Resource.YArchitechIcon;
			this.selectedLocation = locationName;
		}

		private void FormObtainLocation_Load(object sender, EventArgs e)
		{
			this.InitialInterface();
			this.SelectedDefaultButton(this.selectedLocation);
			this.SetFormSizeAndButtonLoc();
		}

		public string GetSelectedLocation()
		{
			return this.selectedLocation;
		}

		private void InitialInterface()
		{
			List<InsulationLocation> allLocations = new OperationLocation().GetAllLocations();
			int locX = 10;
			int num = 10;
			int num2 = 40;
			for (int i = 0; i < allLocations.Count; i++)
			{
				InsulationLocation insulationLocation = allLocations[i];
				if (i % 20 == 0)
				{
					int num3 = i / 20;
					locX = num + num3 * 120 - num3;
				}
				int num4 = i % 20;
				int locY = num4 * 25 - num4 + num2;
				Button value = this.CreatLocButton(locX, locY, insulationLocation.LocationName, insulationLocation.LocationMark);
				base.Controls.Add(value);
				this.lastLocX = locX;
				this.lastLocY = locY;
				if (i == allLocations.Count - 1)
				{
					int num5 = 20 - i % 20;
					for (int j = 0; j < num5; j++)
					{
						locY = (num4 + j) * 25 - (num4 + j) + num2;
						value = this.CreatLocButton(locX, locY, "", "");
						base.Controls.Add(value);
						this.lastLocX = locX;
						this.lastLocY = locY;
					}
				}
			}
		}

		private void SetFormSizeAndButtonLoc()
		{
			int num = this.lastLocY + 25 + 5;
			this.btnCancel.Location = new System.Drawing.Point(this.lastLocX + 35, num);
			this.btnSure.Location = new System.Drawing.Point(this.lastLocX - 35, num);
			base.Size = new Size(this.lastLocX + 146, num + 75);
		}

		private Button CreatLocButton(int locX, int locY, string txtValue, string tagValue)
		{
			Button button = new Button();
			button.Location = new System.Drawing.Point(locX, locY);
			button.Size = new Size(120, 25);
			button.FlatStyle = FlatStyle.Flat;
			button.Text = txtValue;
			button.Tag = tagValue;
			if (tagValue != "市")
			{
				button.Enabled = false;
			}
			button.Click += this.Button_Click;
			return button;
		}

		private void SelectedDefaultButton(string locName)
		{
			foreach (object obj in base.Controls)
			{
				Control control = (System.Windows.Forms.Control)obj;
				if (control is Button)
				{
					Button button = control as Button;
					button.BackColor = this.BackColor;
					if (button.Text == locName && button.Tag.ToString() == "市")
					{
						button.PerformClick();
						button.Select();
						button.Focus();
					}
				}
			}
		}

		private void Button_Click(object sender, EventArgs e)
		{
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			foreach (object obj in base.Controls)
			{
				Control control = (System.Windows.Forms.Control)obj;
				if (control is Button)
				{
					Button button2 = control as Button;
					button2.BackColor = this.BackColor;
					if (button2.Text == button.Text && button2.Tag.ToString() == "市")
					{
						button2.BackColor = Color.LightSkyBlue;
						this.selectedLocation = button2.Text;
					}
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
		}

		private void hyTextBox1_TextChanged(object sender, EventArgs e)
		{
			TextBox textBox = this.hyTextBox1;
			if (textBox == null || string.IsNullOrEmpty(textBox.Text))
			{
				return;
			}
			foreach (object obj in base.Controls)
			{
				Control control = (System.Windows.Forms.Control)obj;
				if (control is Button)
				{
					Button button = control as Button;
					if (button.Text.Contains(textBox.Text) && button.Tag != null && button.Tag.ToString() == "市")
					{
						button.PerformClick();
						button.Select();
						button.Focus();
						break;
					}
				}
			}
		}

		private const int BTNWIDTH = 120;

		private const int BTNHEIGHT = 25;

		private string selectedLocation = "北京";

		private int lastLocX;

		private int lastLocY;
	}
}
